package cn.anc.aonicardv.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by yangdai on 2017/12/1.
 */

public class CircleProgressView extends View {

    private Paint mPaint;

    public CircleProgressView(Context context) {
        super(context);
        init();
    }

    private Handler mHandler = new Handler();

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setStrokeWidth(7);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStyle(Paint.Style.FILL);
    }

    int mAngle;

    public void setProgress(int progress) {
        mAngle = progress;
        invalidate();
    }

    public void setAutoProgress() {
        mHandler.post(updateThread);
    }

    int mAutoAngle = 0;
    Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            if (mAutoAngle == 100) {
                mAutoAngle = 0;
            }
            mAutoAngle += 5;
            mAngle = (int) (360.0 * mAutoAngle / 100);
            invalidate();
            mHandler.postDelayed(updateThread, 50);
        }
    };


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int r = getWidth() / 2;
        int r2 = r - 20;
        for (int i = 1; i <= 24; i++) {
            int y = 0;
            int x = 0;
            if (i * 15 == 90) {
                x = r + r2;
                y = r;
            } else if (i * 15 == 180) {
                x = r;
                y = r + r2;
            } else if (i * 15 == 270) {
                x = 20;
                y = r;
            } else if (i * 15 == 360) {
                x = r;
                y = 20;
            }
            if (i * 15 > 0 && i * 15 < 180) {
                double radian = Math.toRadians(i * 15 % 90);
                double sin = Math.sin(radian);
                double cos = Math.cos(radian);
                if (i * 15 > 0 && i * 15 < 90) {
                    x = (int) (r + (sin * r2));
                    y = (int) (r - (cos * r2));
                } else {
                    x = (int) (r + (cos * r2));
                    y = (int) (r + (sin * r2));
                }
            } else if (i * 15 > 180 && i * 15 < 360) {
                double radian = Math.toRadians(i * 15 % 90);
                double sin = Math.sin(radian);
                double cos = Math.cos(radian);
                if (i * 15 > 180 && i * 15 < 270) {
                    x = (int) (r - (sin * r2));
                    y = (int) (r + (cos * r2));
                } else {
                    x = (int) (r - (cos * r2));
                    y = (int) (r - (sin * r2));
                }
            }
            if (i * 15 == (mAngle - (mAngle % 15))) {
                mPaint.setStrokeWidth(15);
            } else {
                mPaint.setStrokeWidth(7);
            }
            canvas.drawPoint(x, y, mPaint);
        }
    }
}
